package com.wkq.database.utils;

import android.content.Context;

import com.wkq.database.dao.CollectionNewsInfo;
import com.wkq.database.dao.CollectionNewsInfoDao;
import com.wkq.database.dao.NewsComment;
import com.wkq.database.dao.NewsCommentDao;
import com.wkq.database.dao.SignInInfo;
import com.wkq.database.dao.SignInInfoDao;
import com.wkq.database.dao.UserInfo;
import com.wkq.database.dao.UserInfoDao;

import java.util.List;

/**
 * 作者: 吴奎庆
 * <p>
 * 时间: 2019/12/26
 * <p>
 * 简介: 数据库操作工具基类
 */
public class DataBaseUtils {


    /**
     * 插入用户数据
     *
     * @param context
     */

    public static void insertHomeTopData(Context context, String phoneNum, String nickName, String pwd, String path) {
        UserInfoDao dao = DaoHelper.getInstance(context).getUserInfoDao();
        setOtheLoginFalse(context);
        UserInfo info = new UserInfo();
        info.setUserIcon(path);
        info.setUserName(nickName);
        info.setUserPhoneNum(phoneNum);
        info.setUserPwd(pwd);
        info.setIsLogout(true);
        dao.insertOrReplace(info);

    }

    public static void setOtheLoginFalse(Context context) {
        UserInfoDao dao = DaoHelper.getInstance(context).getUserInfoDao();
        List<UserInfo> infos = dao.loadAll();
        if (infos != null) {
            for (UserInfo info : infos) {
                info.setIsLogout(false);
            }
            dao.updateInTx(infos);
        }
    }

    public static void logout(Context context) {
        UserInfoDao dao = DaoHelper.getInstance(context).getUserInfoDao();
        dao.deleteAll();
    }

    public static void initUser(Context context, String phoneNum, String nickName, String pwd, String path) {
        UserInfoDao dao = DaoHelper.getInstance(context).getUserInfoDao();
        if (dao.loadAll() == null || dao.loadAll().size() > 0) return;
        UserInfo info = new UserInfo();
        info.setUserIcon(path);
        info.setUserName(nickName);
        info.setUserPhoneNum(phoneNum);
        info.setUserPwd(pwd);
        info.setIsLogout(false);
        dao.insertOrReplace(info);
    }

    /**
     * 判断用户是否存在
     *
     * @param context
     * @param phoneNum
     * @return
     */
    public static boolean isExistUser(Context context, String phoneNum) {
        UserInfoDao dao = DaoHelper.getInstance(context).getUserInfoDao();
        UserInfo info = dao.load(phoneNum);
        return info == null ? false : true;
    }

    public static UserInfo getUser(Context context) {
        UserInfoDao dao = DaoHelper.getInstance(context).getUserInfoDao();
        List<UserInfo> infos = dao.loadAll();
        for (UserInfo info : infos) {
            if (info.getIsLogout()) {
                return info;
            }
        }
        return null;
    }

    /**
     * 匹配账户
     *
     * @param context
     * @param phoneNum
     * @param pwd
     * @return
     */
    public static boolean checkUser(Context context, String phoneNum, String pwd) {
        UserInfoDao dao = DaoHelper.getInstance(context).getUserInfoDao();
        UserInfo info = dao.load(phoneNum);
        if (info == null) return false;
        if (pwd.equals(info.getUserPwd())) return true;
        return false;
    }

    /**
     * 获取用户数据
     *
     * @param context
     * @return
     */
    public static boolean isLogin(Context context) {
        UserInfoDao dao = DaoHelper.getInstance(context).getUserInfoDao();
        List<UserInfo> infos = dao.loadAll();
        if (infos == null) return false;
        for (UserInfo info : infos) {
            if (info.getIsLogout()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 设置登录状态
     *
     * @param context
     * @param phoneNum
     * @param isLogin
     */
    public static void insertLoginState(Context context, String phoneNum, boolean isLogin) {
        setOtheLoginFalse(context);
        UserInfoDao dao = DaoHelper.getInstance(context).getUserInfoDao();
        UserInfo info = dao.load(phoneNum);
        if (info != null) info.setIsLogout(isLogin);
        dao.update(info);

    }

    public static void updateUserName(Context context, String nickName) {
        UserInfoDao dao = DaoHelper.getInstance(context).getUserInfoDao();
        UserInfo userInfo = null;
        List<UserInfo> infos = dao.loadAll();
        for (UserInfo info : infos) {
            if (info.getIsLogout()) {
                userInfo = info;
                break;
            }
        }
        if (userInfo != null) {
            UserInfoDao dao2 = DaoHelper.getInstance(context).getUserInfoDao();
            userInfo.setUserName(nickName);
            dao2.updateInTx(userInfo);
        }
    }

    public static void updateDesc(Context context, String desc) {
        UserInfoDao dao = DaoHelper.getInstance(context).getUserInfoDao();
        UserInfo userInfo = null;
        List<UserInfo> infos = dao.loadAll();
        for (UserInfo info : infos) {
            if (info.getIsLogout()) {
                userInfo = info;
                break;
            }
        }
        if (userInfo != null) {
            UserInfoDao dao2 = DaoHelper.getInstance(context).getUserInfoDao();
            userInfo.setUserDesc(desc);
            dao2.updateInTx(userInfo);
        }
    }

    public static void updateLocation(Context context, String location) {
        UserInfoDao dao = DaoHelper.getInstance(context).getUserInfoDao();
        UserInfo userInfo = null;
        List<UserInfo> infos = dao.loadAll();
        for (UserInfo info : infos) {
            if (info.getIsLogout()) {
                userInfo = info;
                break;
            }
        }
        if (userInfo != null) {
            UserInfoDao dao2 = DaoHelper.getInstance(context).getUserInfoDao();
            userInfo.setUserLocation(location);
            dao2.updateInTx(userInfo);
        }
    }

    public static void updateUserIcon(Context context, String icon) {
        UserInfoDao dao = DaoHelper.getInstance(context).getUserInfoDao();
        UserInfo userInfo = null;
        List<UserInfo> infos = dao.loadAll();
        for (UserInfo info : infos) {
            if (info.getIsLogout()) {
                userInfo = info;
                break;
            }
        }
        if (userInfo != null) {
            UserInfoDao dao2 = DaoHelper.getInstance(context).getUserInfoDao();
            userInfo.setUserIcon(icon);
            dao2.updateInTx(userInfo);
        }
    }

    public static List<CollectionNewsInfo> getCollectionNewsInfo(Context context) {
        CollectionNewsInfoDao dao = DaoHelper.getInstance(context).getCollectionDao();
        List<CollectionNewsInfo> infos = dao.loadAll();
        return infos;
    }


    public static void collection(Context context, String id, String news_id, String news_type, String news_time, String news_title, String news_icon, String news_author, String news_desc, String type) {
        CollectionNewsInfoDao dao = DaoHelper.getInstance(context).getCollectionDao();
        CollectionNewsInfo userInfo = null;
        if (!isCollection(context, news_id)) {
            userInfo = new CollectionNewsInfo();
            userInfo.setId(id);
            userInfo.setNews_author(news_author);
            userInfo.setNews_id(news_id);
            userInfo.setNews_desc(news_desc);
            userInfo.setNews_icon(news_icon);
            userInfo.setNews_time(news_time);
            userInfo.setTime(System.currentTimeMillis());
            userInfo.setNews_title(news_title);
            userInfo.setNews_type(news_type);
            userInfo.setType(type);
            dao.insertOrReplace(userInfo);
        }
    }

    public static void cancelCollection(Context context, String news_id) {
        CollectionNewsInfoDao dao = DaoHelper.getInstance(context).getCollectionDao();
        List<CollectionNewsInfo> infos = dao.loadAll();
        if (infos != null && infos.size() > 0) {
            for (CollectionNewsInfo info : infos) {
                if (info.getNews_id().equals(news_id)) dao.delete(info);
            }
        }

    }

    public static boolean isCollection(Context context, String news_id) {
        CollectionNewsInfoDao dao = DaoHelper.getInstance(context).getCollectionDao();

        List<CollectionNewsInfo> infos = dao.loadAll();
        if (infos == null || infos.size() == 0) return false;
        for (CollectionNewsInfo info : infos) {
            if (info.getNews_id().equals(news_id)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据年月 获取时间
     *
     * @param context
     * @param years
     * @param mouth
     * @return
     */
    public static List<SignInInfo> getSignInInfos(Context context, String years, String mouth) {
        SignInInfoDao dao = DaoHelper.getInstance(context).getSiginINfoDao();
        List<SignInInfo> list = dao.queryBuilder().where(SignInInfoDao.Properties.S_years.eq(years)).where(SignInInfoDao.Properties.S_mouth.eq(mouth))
                .orderAsc(SignInInfoDao.Properties.Id).list();
        return list;
    }

    /**
     * 根据年月 获取时间
     *
     * @param context
     * @param years
     * @param mouth
     * @return
     */
    public static SignInInfo getSignInInfo(Context context, String years, String mouth, String days) {
        SignInInfoDao dao = DaoHelper.getInstance(context).getSiginINfoDao();
        List<SignInInfo> list = dao.queryBuilder().where(SignInInfoDao.Properties.S_years.eq(years)).where(SignInInfoDao.Properties.S_mouth.eq(mouth)).where(SignInInfoDao.Properties.S_days.eq(days))
                .orderAsc(SignInInfoDao.Properties.Id).list();
        return list.size() == 0 ? null : list.get(0);
    }


    /**
     * 根据年月 获取时间
     *
     * @param context
     * @param years
     * @param mouth
     * @return
     */
    public static void insertSigninInfo(Context context, String years_mounth, String years, String mouth, String days) {
        SignInInfoDao dao = DaoHelper.getInstance(context).getSiginINfoDao();
        SignInInfo info = new SignInInfo();
        info.setS_mouth(mouth);
        info.setS_years(years);
        info.setS_days(days);
        info.setId(1 + "");
        info.setTime(System.currentTimeMillis());
        info.setS_years_mounth_days(years_mounth);
        dao.insertOrReplace(info);
    }

    /**
     * @param context
     * @param news_comment_content
     * @param news_typs
     * @param news_id
     * @param news_comment_user_icon
     * @param news_comment_user_name
     */


    public static void insertNewsComment(Context context, String news_comment_content, String news_typs, String news_id, String news_comment_user_icon, String news_comment_user_name, String news_title, String news_desc, String news_icon) {

        NewsCommentDao dao = DaoHelper.getInstance(context).getNewsCommentDao();
        NewsComment info = new NewsComment();
        info.setNews_comment_content(news_comment_content);
        info.setNews_comment_user_icon(news_comment_user_icon);
        info.setNews_comment_user_name(news_comment_user_name);
        info.setNews_id(news_id);
        info.setNews_typs(news_typs);
        info.setNews_title(news_title);
        info.setNews_desc(news_desc);
        info.setNews_icon(news_icon);
        info.setTime(System.currentTimeMillis());
        dao.insertOrReplace(info);
    }

    /**
     * 获取评论
     * @param context
     * @param news_Id
     * @param news_typs
     * @return
     */
    public static List<NewsComment> getComments(Context context, String news_Id, String news_typs) {

        NewsCommentDao dao = DaoHelper.getInstance(context).getNewsCommentDao();

        return dao.queryBuilder().where(NewsCommentDao.Properties.News_typs.eq(news_typs)).where(NewsCommentDao.Properties.News_id.eq(news_Id)).list();
    }

    public static List<NewsComment> getAllComments(Context context) {

        NewsCommentDao dao = DaoHelper.getInstance(context).getNewsCommentDao();

        return dao.loadAll();
    }

}
